﻿using System;
using System.Reflection;

namespace Nb.Common.Extensions
{
    public static class TypeExtensions
    {
        public static string GetFriendlyName(this Type type)
        {
            string friendlyName = type.Name;
            if (type.IsGenericType)
            {
                int iBacktick = friendlyName.IndexOf('`');
                if (iBacktick > 0)
                {
                    friendlyName = friendlyName.Remove(iBacktick);
                }
                friendlyName += "<";
                Type[] typeParameters = type.GetGenericArguments();
                for (int i = 0; i < typeParameters.Length; ++i)
                {
                    //string typeParamName = typeParameters[i].Name;
                    //friendlyName += (i == 0 ? typeParamName : "," + typeParamName);
                    var subType = typeParameters[i];
                    var subTypeName = GetFriendlyName(subType);
                    friendlyName += (i == 0 ? subTypeName : "," + subTypeName);
                }
                friendlyName += ">";
            }

            return friendlyName;
        }

        public static string GetFriendlyFileName(this Type type)
        {
            string friendlyName = type.Name;
            if (type.IsGenericType)
            {
                int iBacktick = friendlyName.IndexOf('`');
                if (iBacktick > 0)
                {
                    friendlyName = friendlyName.Remove(iBacktick);
                }
                friendlyName += "[";
                Type[] typeParameters = type.GetGenericArguments();
                for (int i = 0; i < typeParameters.Length; ++i)
                {
                    //string typeParamName = typeParameters[i].Name;
                    //friendlyName += (i == 0 ? typeParamName : "," + typeParamName);

                    var subType = typeParameters[i];
                    var subTypeName = GetFriendlyFileName(subType);
                    friendlyName += (i == 0 ? subTypeName : "," + subTypeName);
                }
                friendlyName += "]";
            }

            return friendlyName;
        }

        public static string GetFriendlyNameForMethod(this MethodInfo methodInfo)
        {
            string friendlyName = methodInfo.Name;
            if (methodInfo.IsGenericMethod)
            {
                int iBacktick = friendlyName.IndexOf('`');
                if (iBacktick > 0)
                {
                    friendlyName = friendlyName.Remove(iBacktick);
                }
                friendlyName += "<";
                Type[] typeParameters = methodInfo.GetGenericArguments();
                for (int i = 0; i < typeParameters.Length; ++i)
                {
                    //string typeParamName = typeParameters[i].Name;
                    //friendlyName += (i == 0 ? typeParamName : "," + typeParamName);
                    var subType = typeParameters[i];
                    var subTypeName = GetFriendlyName(subType);
                    friendlyName += (i == 0 ? subTypeName : "," + subTypeName);
                }
                friendlyName += ">";
            }

            return friendlyName;
        }
    }
}
